<?php

namespace Beerus\Modules\Designer\Controllers;

use App\Pockets\GridDataModel as pGridDataModel;
use App\Http\Controllers\Controller;
use Doraemon\model\Designer\DesignerCourse;
use Doraemon\tools\ImgUrl;
use Illuminate\Http\Request;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Tab;
use Illuminate\Support\MessageBag;
use Encore\Admin\Widgets\Form;
use Doraemon\service\User;

class DesignerCourseController extends Controller
{
	const PAGE_SIZE = 20;

	public function __construct()
	{
		parent::__construct();
		Admin::script('$(".pjax-container").attr("id","pjax-container");');
	}

	public function index(Request $request, Content $content)
	{
		$request_data = $request->all();

		$page_size = $request->input('per_page', self::PAGE_SIZE);
		$page = $request->input('page', 1);

		$condition = [];
		if (!empty($request_data['uid'])) {
			$condition['uid'] = $request_data['uid'];
		}

		if (!empty($request_data['nick'])) {
			$uid = User::getUidByNick($request_data['nick']);
			$condition['uid'] = $uid;
		}

		$condition['ORDER'] = ['id' => 'DESC'];
		$condition['LIMIT'] = [
			($page - 1) * $page_size,
			$page_size
		];

		$lists = DesignerCourse::select('*', $condition);

		$total = 0;
		if (!empty($lists)) {
			unset($condition['ORDER']);
			unset($condition['LIMIT']);
			$total = DesignerCourse::count($condition);

			foreach ($lists as &$item) {
				$item['cover'] = ImgUrl::getUrl(
					ImgUrl::APPTYPE_OP,
					$item['cover'],
					ImgUrl::IMAGE_SIZE_SQ_PHONE
				);
			}
			unset($item);
		}

		$gridModel = new pGridDataModel($lists, $total, [
			'perPage' => $page_size
		]);

		$gridList = Admin::grid(
			$gridModel,
			function (Grid $grid) use ($gridModel) {
				$grid->paginate($gridModel->getPerPage());
				$grid->disableExport();
				$grid->disableRowSelector();

				$grid->filter(function (Grid\Filter $filter) {
					$filter->disableIdFilter();
					$filter->column(1 / 2, function ($filter) {
						$filter->equal('note_id', 'Note_id')->placeholder('请输入note id');
					});
					$filter->expand();
				});

				$grid->column('note_id', '视频note_id');

				$grid->column('cover', '封面图')->width(210)->display(function () {
					return "<img width='200' height='200' src='{$this->cover}'>";
				});

				$grid->column('sort', '排序值');
				$grid->column('remark', '备注');

				$grid->column('create_time', '创建时间')->display(function () {
					if (empty($this->create_time)) {
						return '-';
					}
					return date('Y-m-d H:i:s', $this->create_time);
				});

				$grid->column('edit_time', '修改时间')->display(function () {
					if (empty($this->edit_time)) {
						return '-';
					}
					return date('Y-m-d H:i:s', $this->edit_time);
				});

				$grid->column('data_status', '数据状态')->display(function () {
					return DesignerCourse::STATUS[$this->data_status];
				});

				$grid->column('admin_id', '操作人')->display(function () {
					if ($this->admin_id) {
						return Admin::user()->find($this->admin_id)->name;
					} else {
						return '-';
					}
				});

				$grid->actions(function (Grid\Displayers\Actions $actions) {
					$actions->disableDelete();
				});
			}
		);

		$tab = new Tab();
		$tab->add('课程列表', $gridList->render());
		$content->row($tab);

		return $content
			->header('课程列表')
			->description(' ')
			->breadcrumb(['text' => '课程列表']);
	}

	public function edit(Request $request, Content $content, $id = 0)
	{
		$request = $request->all();

		if ($id == 0) {
			$is_create = true;
			$header = '添加课程';
		} else {
			$is_create = false;
			$header = '修改视频';
		}

		$tab = new Tab();
		$tab->add('课程信息', self::editAction($request, $id, $is_create));
		return $content->header($header)
			->breadcrumb([
				'text' => '课程列表',
				'url' => route('course.list')
			], ['text' => $header])->row($tab);
	}

	public function editAction($request, $id = 0, $is_create = false)
	{
		if ($is_create) {
			$form = new Form([
				'sort' => 1
			]);
		} else {
			$data = DesignerCourse::get('*', ['id' => $id]);
			$form = new Form($data);
			$form->hidden('id');
		}

		$form->imgUpload('cover', '封面图');
		$form->text('note_id', '视频note_id')->setWidth(4, 2)->required('不能为空');
		$form->number('sort', '排序位置')->setWidth(4, 2)->required('不能为空');
		$form->textarea('remark', '备注')->setWidth(4, 2);
		$form->select('data_status', '数据状态')->default(DesignerCourse::STATUS_NORMAL)
			->setWidth(4, 2)->options(DesignerCourse::STATUS);
		$form->action('/admin/Designer/course_list/save');

		return $form->render();
	}

	public function editSubmit(Request $request)
	{
		$data = [
			'cover' => $request['cover'],
			'note_id' => $request['note_id'],
			'sort' => $request['sort'],
			'remark' => $request['remark'],
			'data_status' => $request['data_status'],
			'admin_id' => Admin::user()->id,
		];

		if (isset($request['id'])) {
			$data['edit_time'] = time();
			$rs = DesignerCourse::update($data, [
				'id' => $request['id']
			]);
		} else {
			$data['create_time'] = time();
			$rs = DesignerCourse::insert($data);
		}

		if ($rs) {
			$success = new MessageBag([
				'title' => '操作成功'
			]);

			return redirect('/admin/Designer/course_list')->with(compact('success'));
		} else {
			$error = new MessageBag([
				'title' => '操作失败'
			]);

			return back()->with(compact('error'));
		}
	}
}
